package com.cr.api.controller;

import com.cr.api.model.AddCommunicationModel;
import com.cr.api.vo.CommunicationVo;
import com.cr.business.domain.CrCommunication;
import com.cr.business.service.ICrCommunicationService;
import com.cr.common.core.controller.BaseController;
import com.cr.common.core.domain.AjaxResult;
import com.cr.common.utils.ConvertUtil;
import com.cr.common.utils.MyStringUtils;
import com.github.pagehelper.PageInfo;
import java.util.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(description = "留言沟通")
@RestController
@RequestMapping({ "/api/communication" })
@CrossOrigin
public class ApiCommunicationController extends BaseController {
    @Autowired
    private ICrCommunicationService communicationService;

    /**
     * 留言沟通列表
     */
    @ApiOperation("留言沟通列表")
    @PostMapping("/getCommunicationList")
    @ResponseBody
    public AjaxResult getNoticeList(@RequestBody CrCommunication crCommunication) {
        if (crCommunication.getUserId() == null)
            return AjaxResult.error("用户id不能为空");
        startPage();
        List<CrCommunication> communicationList = communicationService.selectCrCommunicationList(crCommunication);
        List<CommunicationVo> list = ConvertUtil.convertList(communicationList, CommunicationVo.class);
        return AjaxResult.success(new PageInfo<>(list));
    }

    @PostMapping({ "/addCommunication" })
    @ResponseBody
    @ApiOperation("留言沟通")
    public AjaxResult addCommunication(@RequestBody AddCommunicationModel model) {
        if (model.getUserId() == null)
            return AjaxResult.error("用户id不能为空");
        if (MyStringUtils.isEmpty(model.getContent()))
            return AjaxResult.error("输入内容不能为空");
        CrCommunication communication = new CrCommunication();
        BeanUtils.copyProperties(model, communication);
        communication.setCreateBy(model.getUserId().toString());
        communication.setCreateTime(new Date());
        return toAjax(this.communicationService.insertCrCommunication(communication));
    }

}
